package com.iflytec.download;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 线程池工具方法
 */
public class ThreadPoolUtil {

    public static ThreadPoolExecutor newThreadPool(int threadCount, String threadNamePrefix) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(threadCount, threadCount, 60, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1000), new ThreadPoolExecutor.CallerRunsPolicy());

        threadPoolExecutor.setThreadFactory(new ThreadFactory() {
            private final AtomicInteger count = new AtomicInteger(1);

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, threadNamePrefix + count.getAndIncrement());
            }
        });
        return threadPoolExecutor;
    }


}
